const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
} 

var getNextTime=function(minnum,date){
        //1. js获取当前时间
  if (!date) { date = new Date(); } else { if (!isNaN(date)) { date = new Date(date) } else if (date instanceof Date) { } else { date = new Date(date)}} 
  if (!minnum) minnum=0;
  var min = date.getMinutes();  //2. 获取当前分钟
  date.setMinutes(min + minnum);  //3. 设置当前时间+10分钟：把当前分钟数+10后的值重新设置为date对象的分钟数
  var y = date.getFullYear();
  var m = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1);
  var d = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
  var h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
  var f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
  var s = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
  var formatdate = y + '-' + m + '-' + d + " " + h + ":" + f ;
  return formatdate;
}

var fetchGet = function (url, callback, errorback) {
  request('GET', url, null, callback, errorback);
}

var url={
  "shop_food_list":"/shop/food/list",
  "shop_food_liston": "/shop/food/liston",
  "shop_food_detail": "/shop/food/detail",
  "shop_food_add": "/shop/food/add",
  "shop_food_updatefoodstate": "/shop/food/updatefoodstate",
  "shop_food_typelist": "/shop/food/typelist",
  "shop_food_typeadd": "/shop/food/typeadd",

  "shop_order_list": "/shop/order/orderlist",
  "shop_order_id": "/shop/order/getorderbyid",
  "shop_order_makefood": "/shop/order/makefood",
  "shop_order_takefood": "/shop/order/takefood",
  "shop_order_orderstate":"/shop/order/orderstate",
  "shop_order_monstatis":"/shop/order/monstatis",
  "shop_order_daystatis": "/shop/order/daystatis", 
  "shop_order_addtips":'/shop/order/addtips',
  "shop_order_refused":'/shop/order/refused',
  "shop_order_refund":'/shop/order.refund',
  

  "shop_redpacket_addredact": "/shop/redpacket/addredact",
  "shop_redpacket_listredact": "/shop/redpacket/listredact",
  "shop_redpacket_actid": "/shop/redpacket/getredactbyid",

  "shop_shop_getshop": "/shop/shop/getshop",
  "shop_shop_addshop": "/shop/shop/add",
  "shop_shop_setshop": "/shop/shop/set",

  "food_type_list":"/food/typelist",
  "food_list": "/food/liston",

  "order_create":"/foodorder/create",
  "order_pay": "/foodorder/payorder",
  "order_list": "/foodorder/listorder",
  "order_copy": "/foodorder/copyorder",
  "order_history":"/foodorder/history",
  "order_id": "/foodorder/id",
  "order_setredpacket":"/foodorder/setredpacket",
  "order_cancle":"/foodorder/cancle",
  "order_confirm":"/foodorder/confirm",

  "redpacket_list":"/redpacket/list",
  "redpacket_liston": "/redpacket/liston",
  "redpacket_get_fromact": "/redpacket/getpacketfromact",
  "redpacket_getonlineact": "/redpacket/getonlineact",

  "getshop":"/ushop/getshop",
  "getsholist":'/ushop/shopnear',

  "collect_add":"/collect/add",
  "collect_cancle": "/collect/cancle",
  "collect_list": "/collect/list",
  "collect_list_id": "/collect/listid",

  "user_add":"/weiuser/add",
  "user_getopenid":"/weiuser/getOpenId",

  "websocket":"/websocket",

  "user_suggest":"/sugg/suggest",
  "user_shop_apply":"sugg/apply",

  "user_evaluate_add":"/eval/add",
  "user_evaluate_list":"/eval/list",
  "user_evaluate_getbyorder":"/eval/geteval",

  "user_address_list":"/address/list",
  "user_address_add": "/address/add",
  "user_address_setdefault": "/address/setdefault",
  "user_address_getdefault":"/address/getdefault",


  "user_delivery_preorder":"/delivery/preorder",
  "user_delivery_getwaycost":"/delivery/getwaycost"
}
var sysinfo = '';
// post请求方法
function fetchPost(url, data, callback,errorback) {
  request('POST',url,data,callback,errorback);
}
var preurl="";
var request = function (method,url,data,callback,errorback){
  if (preurl == url){
    showinfo("程序正在处理中，请等待");
    return ;
  }
  preurl=url;
  var weiuser = wx.getStorageSync('weiuser');
  var header = { 'content-type': 'application/json' };
  if (weiuser) {
    header.userid = weiuser.id;
  }
  var shopid = wx.getStorageSync('shopid');
  if (shopid) {
    header.shopid = shopid;
  }
  sysinfo = getSystemInfo();
  if (sysinfo&&sysinfo!=''){
    header.sysinfo = JSON.stringify(sysinfo);
  }
  wx.request({ 
    method: method,
    url: wx.getStorageSync('host') + url,
    header: header,
    data: data,
    success(res) {
      preurl="";
      if (res.data.error && res.data.error.code != 0) {
        
        showinfo(res.data.error.msg);
        if (errorback){
          errorback(res.data.error);
        }
      } else {
        callback(res.data)
      }

    },
    fail(e) {
      preurl="";
      showinfo('连接服务器出错', 'error');
      console.error(e)
    }
  })
}

var setTabBadge=function(index,num){
  return;
  if (num>0){
    wx.setTabBarBadge({
      index: index,
      text: num + ""
    })
  }else{
    wx.removeTabBarBadge({index});
  }
}
var showinfo=function(msg,icon){
  if (!icon) icon = 'none'
  wx.showToast({
    title: msg,
    icon: icon,
    duration: 2000
  })
}

var getSystemInfo=function(){ 
  if (sysinfo != undefined && sysinfo != '') return sysinfo;
  const ret = wx.getSystemInfoSync()
    var sys = { br: ret.brand}
    sys.model = ret.model
    sys.version = ret.version
    sys.system = ret.system
    sys.platform = ret.platform
    sysinfo= sys;
    return sys;
  
}
module.exports = {
  formatTime: formatTime,
  fetchGet: fetchGet,
  fetchPost: fetchPost,
  setTabBadge: setTabBadge,
  showinfo: showinfo,
  getNextTime: getNextTime,
  url:url
}
